package com.example.demo.common.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;

public class SequenceCodeUtil {
	public static void main(String[] args) {
		
	}

	public static synchronized String getContactCode(int maxNum,String qianzhui) {
		StringBuilder builder = new StringBuilder();
		StringBuilder num = new StringBuilder();
		Date nowDate = new Date();// 获取当前时间
		String stringDateFormat = "yyyyMMdd";// 格式化当前时间
		SimpleDateFormat sdf = new SimpleDateFormat(stringDateFormat);// 定义时间编译器
		String localNowDate = sdf.format(nowDate);// 定义String类型值
		AtomicInteger count = new AtomicInteger(maxNum);
		String prefix = qianzhui + localNowDate;
		// 6位数字的采取编号处理。99999的情况下从00001开始采取。
		if (count.get() > 999999) {
			prefix = "ClUE" + localNowDate;
			System.out.println(prefix);
			count = new AtomicInteger(1);
		} // 采用4位数的数字进行序号处理。
		if (count.get() < 10) {
			num.append("00000").append(count.getAndIncrement());
		} else if (count.get() < 100) {
			num.append("0000").append(count.getAndIncrement());
		} else if (count.get() < 1000) {
			num.append("000").append(count.getAndIncrement());
		} else if (count.get() < 10000) {
			num.append("00").append(count.getAndIncrement());
		} else if (count.get() >= 100000) {
			num.append(count.getAndIncrement());// 判断大于10万就不拼接0
		} else {
			num.append("0").append(count.getAndIncrement());
		} // 组合。
		builder.append(prefix);

		builder.append(num);
		return builder.toString();
	}
}
